package com.jstyle.jclife.adapter;

import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.android.flexbox.FlexboxLayoutManager;
import com.jstyle.jclife.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/6/13.
 */

public class HistoryShowAdapter extends RecyclerViewBaseAdapter<String> {

   List<String>stepData=new ArrayList<>();


    public HistoryShowAdapter(List<String> mDatas) {
        super(mDatas);
    }
    boolean showBottomView=true;
    public void showBottomView(boolean enable){
        this.showBottomView=enable;
    }

    @Override
    protected void bindData(BaseViewHolder holder, int position) {
        NormalViewHolder normalViewHolder=(NormalViewHolder)holder;
        normalViewHolder.tvTitleHistoryDate.setText(mDataList.get(position));
        if(position<stepData.size()){
            String data=stepData.get(position);
            if(!TextUtils.isEmpty(data)) normalViewHolder.tvDataHistoryDay.setText(data);
        }
        int visibility=getVisibility(position);
        normalViewHolder.viewVertical.setBackgroundColor(dividerColor);
        normalViewHolder.viewHorizontal.setBackgroundColor(dividerColor);
        normalViewHolder.viewHorizontal.setVisibility(visibility);
        normalViewHolder.viewVertical.setVisibility(  showBottomView?View.VISIBLE: View.INVISIBLE);
        ViewGroup.LayoutParams lp = ((RelativeLayout) (normalViewHolder.tvDataHistoryDay.getParent())).getLayoutParams();
        if (lp instanceof FlexboxLayoutManager.LayoutParams) {
            FlexboxLayoutManager.LayoutParams flexboxLp = (FlexboxLayoutManager.LayoutParams) lp;
            flexboxLp.setFlexBasisPercent(getPercent(position));
        }

    }
    int dividerColor= Color.parseColor("#86878C");
    public void setDividerColor(int color){
        this.dividerColor=color;
    }

    public class NormalViewHolder extends BaseViewHolder {
        @BindView(R.id.tv_data_history_day)
        TextView tvDataHistoryDay;
        @BindView(R.id.view_Horizontal)
        View viewHorizontal;
        @BindView(R.id.view_Vertical)
        View viewVertical;
        @BindView(R.id.tv_title_history_date)
        TextView tvTitleHistoryDate;
        public NormalViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(getLayoutId(viewType), parent, false);
       BaseViewHolder baseViewHolder = new NormalViewHolder(view);
        return baseViewHolder;
    }

    private float getPercent(int position) {
        float percent = 0;
        if (mDataList.size() == 5) {
            percent = position < 3 ? 0.333f : 0.5f;
        } else if (mDataList.size() == 3) {
            percent = 0.333f;//1/3有机型平分不了
        } else if (mDataList.size() == 4 || mDataList.size() == 2) {
            percent = 0.5f;
        }
        return percent;
    }

    private int getVisibility(int position) {

        int visibility = View.VISIBLE;
        if (mDataList.size() == 5) {
            visibility = position == 2 || position == 4 ? View.INVISIBLE : View.VISIBLE;
        } else if (mDataList.size() == 4) {
            visibility = position == 1 || position == 3 ? View.INVISIBLE : View.VISIBLE;
        } else if (mDataList.size() == 3) {
            visibility = position == 2 ? View.INVISIBLE : View.VISIBLE;
        } else if (mDataList.size() == 2) {
            visibility = position == 1 ? View.INVISIBLE : View.VISIBLE;
        }
        return visibility;
    }


    @Override
    protected int getLayoutId(int viewType) {
        return R.layout.item_history_show;
    }

    public void setData(List<String> data) {
        this.mDataList = data;
        notifyDataSetChanged();
    }
    public void setStepData(List<String>datas) {
        this.stepData = datas;
        notifyDataSetChanged();
    }

}
